<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/6
 * Time: 13:06
 */

namespace App\Http\Controllers\Backstage;

use App\Http\Controllers\Controller;
use App\Http\Controllers\Validator\AdminVail;
use App\Http\Controllers\Validator\CustomersVail;
use App\Model\Account\Type;
use App\Model\Backstage\Customers;
use App\Model\Backstage\RoomWordbook;
use App\Model\Backstage\User;
use App\Tool\Error;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Validator;
use Illuminate\Support\Facades\DB;
use App\Model\Account\SystemType;
use Illuminate\Support\Facades\Redis;

class CustomersController extends Controller
{
    /**
     * 打开房源列表之前  检索条件里面的类型
     */
    public function getListType()
    {
        $area_list = DB::table('area')->where(['parentId' => $this->city_code])->get();
        $branch_info = Auth::user();
        $data = [
            'province_code'     => $this->province_code,//省code
            'city_code'         => $this->city_code,//市code
            'city_name'         => $this->city,//市名字
            'store_id'          => $branch_info['u_d_id'],//部门ID
            'uid'               => $branch_info['u_id'],//用户ID
            'lastday'           => date('Y-m-d',strtotime('-1 month')),//上个月时间
            'day'               => date('Y-m-d',time()),//当天时间
            'area_list'         => $area_list,//区县下拉框
        ];
        $data['grade'] =  SystemType::storageRedis(409); //等级《优质 中等..》
        $data['use'] =  SystemType::storageRedis(1); //用途《住宅 商住  商铺...》
        $data['intention'] =  SystemType::storageRedis(435); //意向  《急迫 随意 ...》
        $data['term'] =  SystemType::storageRedis(440); //期限  《2天内  3天内 ...》
        $data['status'] =  SystemType::storageRedis(454); //状态《有效  预定...》

        $data['roomtype'] =  SystemType::storageRedis(464);//房型《0 1 ...》
//        $data['numlayers'] =  SystemType::storageRedis(219);//层数
        $data['oriented'] =  SystemType::storageRedis(565);//朝向<>南北  东西...>
        /**
         * 类型
         */
        //《住宅、商住、其他》--- 类型 537
        $data['zsq_type'] =  SystemType::storageRedis(537);

        //《商铺  铺厂 网点》----类型 580
        $data['spw_type'] =  SystemType::storageRedis(580);

        //《写字楼  写厂》-----类型  608
        $data['xx_type'] =  SystemType::storageRedis(608);

        //厂房 仓库-----类型  631
        $data['cc_type'] =  SystemType::storageRedis(631);

        //地皮-----类型  660
        $data['dp_type'] =  SystemType::storageRedis(660);

        //车位---------类型  685
        $data['c_type'] =  SystemType::storageRedis(685);

        //类别----420
        $data['category'] = SystemType::storageRedis(420);


        return $this->success('ok', $data);
    }

    /** 客源列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function listCustomers(Request $request,Customers $customers, User $user)
    {
        $parameter = $request->all();
        $where = [];

        $where[] = ['c_status', '=', 1];//过滤非回收站房源
        //获取公司的ID
//        $branchId = 2;
//        $where[] = ['c_branch_id', '=', $branchId];

        //区县ID
        if ($request->input('c_secondary_area')){
            $where[] = ['c_secondary_area', $parameter['c_secondary_area']];
        }
        //房型
        if ($request->input('c_room')){
            $where[] = ['c_room',  $parameter['c_room']];
        }
        //交易类型 1求购 2求租  451求购 452求租 453租购
         if($request->input('c_trading_type') == 1){
            $where[] = ['c_trading_type','<>',452];
            //购房屋价格 最小值
            if ($request->input('c_price_min')){
                $where[] = ['c_price_min', '>=', $parameter['c_price_min']];
            }
            //购房屋价格 最大值
            if ($request->input('c_price_max')){
                $where[] = ['c_price_max', '<=', $parameter['c_price_max']];
            }
        }elseif ($request->input('c_trading_type') == 2) {
            $where[] = ['c_trading_type','<>',451];
            //租房价格最小值
            if($request->input('c_rent_price_min')){
                $where[] = ['c_rent_price_min','>=',$parameter['c_rent_price_min']];

            }
            //租房价格最大值
            if($request->input('c_rent_price_max')){
                $where[] = ['c_rent_price_max','<=',$parameter['c_rent_price_max']];
            }
        } else {
             if ($request->input('c_price_min')){
                 $where[] = ['c_price_min', '>=', $parameter['c_price_min']];
             }
             //购房屋价格 最大值
             if ($request->input('c_price_max')){
                 $where[] = ['c_price_max', '<=', $parameter['c_price_max']];
             }
             if($request->input('c_rent_price_min')){
                 $where[] = ['c_rent_price_min','>=',$parameter['c_rent_price_min']];

             }
             //租房价格最大值
             if($request->input('c_rent_price_max')){
                 $where[] = ['c_rent_price_max','<=',$parameter['c_rent_price_max']];
             }
         }
        //客源状态
        if ($request->input('c_buy_status')){
            $where[] = ['c_buy_status',  $parameter['c_buy_status']];
        }
        //用途
        if ($request->input('c_use_type')){
            $where[] = ['c_use_type',  $parameter['c_use_type']];
        }
        //日期 1：委托日期   2：最后跟进日期   3：首次录入时间  前台直接传时间戳

        //刚进列表  默认查询  录入时间  一个月之内的数据
        if (!$request->input('date_type')) {
            $where[] = ['c_created_at', '>=', strtotime(date('Y-m-d',strtotime('-1 month')).' 00:00:00')];
            $where[] = ['c_created_at', '<=', time()];
        }
        //首次录入时间
        if ($request->input('date_type') == 1){
            if ($parameter['start_time']) {
                $where[] = ['c_created_at', '>=', strtotime($parameter['start_time'].' 00:00:00')];
            }
            if ($parameter['end_time']) {
                $where[] = ['c_created_at', '<=', strtotime($parameter['end_time'].' 23:59:59')];
            }
        }
        //最后跟进时间
        if ($request->input('date_type') == 2){
            if ($parameter['start_time']) {
                $where[] = ['c_followup_time', '>=', strtotime($parameter['start_time'].' 00:00:00')];
            }
            if ($parameter['end_time']) {
                $where[] = ['c_followup_time', '<=', strtotime($parameter['end_time'].' 23:59:59')];
            }
        }
        //委托时间
        if ($request->input('date_type') == 3){
            if ($parameter['start_time']) {
                $where[] = ['c_entrust_time', '>=', strtotime($parameter['start_time'].' 00:00:00')];
            }
            if ($parameter['end_time']) {
                $where[] = ['c_entrust_time', '<=', strtotime($parameter['end_time'].' 23:59:59')];
            }
        }


        //房屋类型
        if ($request->input('c_room_type')){
            $where[] = ['c_room_type', '=', $parameter['c_room_type']];
        }
        //房屋朝向
        if ($request->input('c_toward')){
            $where[] = ['c_toward', '=', $parameter['c_toward']];
        }
        //类别（职业）
        if($request->input('c_professional_type')){
            $where[] = ['c_professional_type','=',$parameter['c_professional_type']];
        }


        //房屋面积 最小值
        if ($request->input('c_acreage_min')){
            $where[] = ['c_acreage_min', '>=', $parameter['c_acreage_min']];
        }
        //房屋面积 最大值
        if ($request->input('c_acreage_max')){
            $where[] = ['c_acreage_max', '<=', $parameter['c_acreage_max']];
        }
        //公私类型
        if ($request->input('c_private_guest')){
            $where[] = ['c_private_guest', '=', $parameter['c_private_guest']];
        }
        //门店ID
        if ($request->input('c_store_id')){
            $where[] = ['c_store_id', '=', $parameter['c_store_id']];
        } elseif (Auth::user()['u_d_id'])  {
            $where[] = ['c_store_id',Auth::user()['u_d_id']];

        }else{
            $apptoken = $parameter['apptoken'];
            $data = explode(',', Redis::get($apptoken));
            $store_id  = $data[0];   //部门id
            $where[] = ['c_store_id', $store_id];
        }
        //员工ID
        if ($request->input('c_employees_id')){
            $where[] = ['c_employees_id', '=', $parameter['c_employees_id']];
        } elseif (!$request->input('c_store_id') && !$request->input('c_employees_id') && Auth::user()['u_id']) {
            $where[] = ['c_employees_id',Auth::user()['u_id']];
        }elseif (!$request->input('c_store_id') && !$request->input('c_employees_id') && !Auth::user()['u_id']) {
            $apptoken = $parameter['apptoken'];
            $data = explode(',', Redis::get($apptoken));
            $uid  = $data[2];   //员工ID
            $where[] = ['c_employees_id', $uid];
        }

        if ($request->input('c_store_id') && !$request->input('c_employees_id')) {

        }

        //智能查询(keywords)   客户名字   客户手机号  地址
        $keywords = '';
        if ($request->input('keywords')){
            $keywords = trim($request->input('keywords'));
        }
        $list = $customers->listCustomers(env('PAGE_LIMIT_ADMIN'), $where, $keywords, 0);//传0代表是PC 端的
         if (!$list) {
            return $this->error(Error::NULL_DATA);
        }


        foreach ($list as $key=>$val) {
//            $val->c_room  = SystemType::getTypeDetail($val->c_room);//房型
            $val->c_entrust_time = $val->c_entrust_time != null ? date('Y-m-d',$val->c_entrust_time) : '';//委托时间
            $val->c_secondary_area_name   = AreaController::getAreaName($val->c_secondary_area);//区县
            $val->c_trading_type_name      = SystemType::getTypeDetail($val->c_trading_type);//交易类型
            $val->c_buy_status_name        = SystemType::getTypeDetail($val->c_buy_status);//购买状态
            $val->c_source_type_name       = SystemType::getTypeDetail($val->c_source_type);//来源
            $val->c_entrust_type_name      = SystemType::getTypeDetail($val->c_entrust_type);//委托方式
            $val->c_user_level_name        = SystemType::getTypeDetail($val->c_user_level);//等级
            $val->c_live_type_name         = SystemType::getTypeDetail($val->c_live_type);//现住
            $val->c_professional_type_name = SystemType::getTypeDetail($val->c_professional_type);//类别
            $val->c_user_intention_name    = SystemType::getTypeDetail($val->c_user_intention);//意向
            $val->c_time_limit_name        = SystemType::getTypeDetail($val->c_time_limit);//期限
            $val->c_use_type_name          = SystemType::getTypeDetail($val->c_use_type);//用途
            $val->c_room_type_name         = SystemType::getTypeDetail($val->c_room_type);//房屋类型
            $val->c_toward_name            = SystemType::getTypeDetail($val->c_toward);//朝向
            $val->c_decorate_type_name     = SystemType::getTypeDetail($val->c_decorate_type);//装修
            $val->c_payment_type_name      = SystemType::getTypeDetail($val->c_payment_type);//付款
            //配套名字 c_complete
            $c_complete_name = array();//配套名字
            $completeArr = explode(',', $val->c_complete);
            foreach ($completeArr as $ke=>$va) {
                array_push($c_complete_name, SystemType::getTypeDetail($va));
            }
            $val->c_complete_name = implode(',', $c_complete_name);//配套的名字
            $val->c_pay_type_name      = SystemType::getTypeDetail($val->c_pay_type);//付佣
            $val->customerInfo = $customers->customersFastInfo($val->c_id);//快速预览信息与跟进
            $val->customerFollow = $customers->fastcCustomerFollow($val->c_id);//获取跟进最大5条

        }

       return $this->success('ok', $list);
    }

    //客源快速预览信息
    public function customerQuickPreview(Request $request,Customers $customers){
        if(!$cId = $request->input('c_id')){
            return $this->error(Error::PARAM_MISSING);
        }
        //快速预览信息
        $data['customersFastInfo'] = $customers->customersFastInfo($cId);
        return $this->success('ok', $data);

    }
    /*
     * Wu Ning
     * 客源跟进
     */
    public function customerFollow(Request $request,Customers $customers)
    {
        $customersId = $request->input('cf_c_id');
        $list = $customers->customersFollow($customersId, env('PAGE_LIMIT_ADMIN'), 0);//PC 传0
//        if (!$list) {
//           return $this->error(Error::CONTENT_LESS_NULL);
//        }

        return $this->success('ok', $list);
    }

    /*
     * 点击编辑客源时 智能匹配房源
     * Wu Ning
     */
    public function customersRecommendRoom(Request $request,Customers $customers)
    {
        if(!$c_id = $request->input('c_id')){
            return $this->error(Error::PARAM_MISSING);
        }
        //查询客源匹配信息
        $data = $customers->getCustomerInfo($c_id);
        $params['c_trading_type'] = $data->c_trading_type;//交易类型( 0求购 1求租 2租购)
//        $params['c_secondary_area'] = $data->c_secondary_area;//二级区域ID
//        $params['c_room'] = $data->c_room;  //c_room
        $params['c_use_type'] = $data->c_use_type;//用途类型(1住宅 2商
        $params['c_acreage_max'] = $data->c_acreage_max;//面积区间(最大值)
        $params['c_acreage_min'] = $data->c_acreage_min;//面积区间(最小值)
        $params['c_price_max'] = $data->c_price_max; //购房价格最大值
        $params['c_price_min'] = $data->c_price_min;//购房价格最小值
        $params['c_rent_price_max'] = $data->c_rent_price_max;//最高租价
        $params['c_rent_price_min'] = $data->c_rent_price_min;//最低租价
        $data = [];
        //匹配房源
        $list  = $customers->customersMatchingRoom($params);

        $data['room_list'] = $list;

        $data['c_id'] = $c_id;
        //快速预览信息
        $data['customersFastInfo'] = $customers->customersFastInfo($c_id);

        return $this->success('ok', $data);
    }

    //添加、修改客源信息之前  查看该手机号的客源信息是否已经存在
    public function repeatAddition(Request $request,Customers $customers)
    {
        $params = $request->except('token');
        $validator = Validator::make($params, [
            'c_user_phone'   => 'required',
        ]);
        if ($validator->fails()) {
            return $this->error(Error::PARAM_MISSING);
        }
        $c_user_phone = $request->input('c_user_phone');
        $c_id = 0;
        if ($request->input('c_id')) {
            $c_id = $request->input('c_id');//修改有客源ID    添加没有客源ID
        }
        $cid = $customers->whether($c_user_phone, $c_id);
        if ($cid){
            $info = $this->getCustomersInfo($cid);
            if ($info->c_entrust_time) {
                $info->c_entrust_time = substr($info->c_entrust_time,0,10);
            }
            return $this->error('error', $info);
        }
        return $this->success('ok', '可以添加');
    }

    //查看录入 修改  有相同手机号客源的信息
    public function getCustomersInfo($cid)
    {
        $customers = new Customers();
        $list = $customers->customersSameList($cid);
        return $list;
    }




    /**添加客源信息
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function addCustomers(Request $request)
    {

        $params = $request->except('token');
        $validator = CustomersVail::add($params);
        if (!$validator) {
            return $this->error(Error::PARAM_MISSING);
        }
        $customers = new Customers();
        $json_info = $request->input('c_room_json','{}');
        $params['c_room_json'] = json_encode($json_info);
        $params['c_entrust_time'] = strtotime($request->input('c_entrust_time'));
        $cId = $customers->addCustomers($params);
       // dd($cId);
        if($cId == -1){
            return $this->error(Error::REPEAT_PHONE);
        }
        if (!$cId) {
            return $this->error(Error::ADD_ERROR);
        }
        //匹配房源
        $data['room_list'] = $customers->customersMatchingRoom($params);
        $data['c_id'] = $cId;
        //快速预览信息
        $data['customersFastInfo'] = $customers->customersFastInfo($cId);
        return $this->success('ok', $data);

    }


    /**修改客源信息
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function updatecustomers(Request $request)
    {
        $params = $request->except('token','c_secondary_area_name','c_trading_type_name','c_buy_status_name','c_source_type_name','c_entrust_type_name',
                        'c_user_level_name','c_live_type_name' ,'c_professional_type_name','c_user_intention_name','c_time_limit_name',
                        'c_use_type_name','c_room_type_name','c_toward_name','c_decorate_type_name','c_payment_type_name',
                        'c_complete_name','c_pay_type_name','name','u_username', 'customerInfo', 'customerFollow',
            'c_position_name', 'c_planning_name', 'c_level_name', 'c_situation_name', 'c_fence_name', 'c_number_of_layers_name', 'c_wc_name', 'c_lot_name');
        $validator = CustomersVail::edit($params);
        if (!$validator) {
            return $this->error(Error::PARAM_MISSING);
        }
        $customers = new Customers();
        $json_info = $request->input('c_room_json','{}');
        $params['c_room_json'] = json_encode($json_info);
        $status = $customers->updateCustomers($params);

         if($status === -1){
            return $this->error(Error::NEED_PER_DISTRIBUTION);
        }
        if($status === -2){
            return $this->error(Error::REPEAT_PHONE);
        }
        if (!$status) {
            return $this->error(Error::EDIT_ERROR);
        }
        //匹配房源
        $list = $customers->customersMatchingRoom($params);
//        $roomWordbook = new RoomWordbook();
//        if($list) {
//            foreach ($list as $k => $v) {
//                $v->building_s = date('Y', $v->building_s);
//                $v->building_name = $roomWordbook->getWordbook($v->building_id); //楼盘字典
//                $v->use_name = SystemType::getTypeDetail($v->use_type);         //用途
//                $v->housing_name = SystemType::getTypeDetail($v->housing_types);  //房屋类型
//                $v->type_of_house_name = SystemType::getTypeDetail($v->type_of_house_sale);  //房屋出售类型
//                $v->disc_name = SystemType::getTypeDetail($v->disc_type);    //盘类型  私盘  公盘
//                $v->trading_name = SystemType::getTypeDetail($v->trading_type); //交易类型
//                $v->trading_state_name = SystemType::getTypeDetail($v->status); //交易状态类型
//                $v->rents_name = SystemType::getTypeDetail($v->rents_type);            //租价类型
//                $v->taxes_fees_name = SystemType::getTypeDetail($v->taxes_fees);            //包税费类型
//                $v->entrust_name = SystemType::getTypeDetail($v->entrust_way);    //委托方式
//                $v->source_name = SystemType::getTypeDetail($v->source_type);    //来源id
//                $v->status_quo_name = SystemType::getTypeDetail($v->status_quo);    //现状
//                $v->decoration_name = SystemType::getTypeDetail($v->decoration);    //装修
//                $v->furniture_name = SystemType::getTypeDetail($v->furniture);    //家具
//                $v->appliance_name = SystemType::getTypeDetail($v->appliance);    //家电
//                $v->rights_name = SystemType::getTypeDetail($v->rights_type);    //产权类型
//                $v->certificate_name = SystemType::getTypeDetail($v->certificate_type);    //证件类型
//                $v->payment_name = SystemType::getTypeDetail($v->payment_type);    //付款类型
//                $v->pay_on_name = SystemType::getTypeDetail($v->pay_on_type);    //付拥类型
//                $v->fence_type_name = SystemType::getTypeDetail($v->fence_type);    //围墙类型
//                $v->wc_name = SystemType::getTypeDetail($v->wc_type);    //厕所类型
//                $v->dorm_name = SystemType::getTypeDetail($v->dorm_type);    //宿舍类型
//                $v->lot_name = SystemType::getTypeDetail($v->lot_type);    //地段类型
//                $v->air_name = SystemType::getTypeDetail($v->air_type);    //空调类型
//                $v->canteen_name = SystemType::getTypeDetail($v->canteen_type);    //食堂类型
//                $v->checking_way_name = SystemType::getTypeDetail($v->checking_way);    //看房方式
//                //多选框数据========配套
//                $support_name = [];
//                if ($v->support) {
//                    $support_arr = explode(',', $v->support);
//                    foreach ($support_arr as $key => $val) {
//                        $support = SystemType::getTypeDetail($val);    //'配套
//                        array_push($support_name, $support);
//                    }
//                }
//                $v->support_name = implode(',', $support_name);
//                //多选框数据========行业
//                $industry_name = [];
//                if ($v->industry_type) {
//                    $industry_arr = explode(',', $v->industry_type);
//                    foreach ($industry_arr as $key => $val) {
//                        $industry = SystemType::getTypeDetail($val);
//                        array_push($industry_name, $industry);
//                    }
//                }
//                $v->industry_name = implode(',', $industry_name);
//                //根据用途变换的输入款  将数据库json字符串转换成数组
//                $v->r_info = json_decode($v->r_info, true);
//                //房源明细列表
//                $v->r_detaile_list = json_decode($v->r_detaile_list, true);
//            }
//        }
        $data['room_list'] = $list;
        $data['c_id'] = $params['c_id'];
        $data['customersFastInfo'] = $customers->customersFastInfo($params['c_id']);
        $data['follow'] = $customers->customersFollow($params['c_id'], env('PAGE_LIMIT_ADMIN'));
        return $this->success('ok', $data);

    }

    /**删除客源信息到回收站 与 恢复
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function recycleBin(Request $request)
    {
        $cId = $request->input('c_id');
        if(!$cId){
            return $this->error(Error::PARAM_MISSING);
        }
        $customers = new Customers();
        $status = $customers->recyclebinCustomers($cId);
        if($status === -1){
            return $this->error(Error::OPERATION_ERROR);
        }
        if (!$status) {
           return  $this->error(Error::DEL_ERROR);
        }
        return $this->success('ok', '操作成功');
    }

    /**删除客源信息（硬删除）
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function del(Request $request)
    {
        $cId = $request->input('c_id');
        //dd($cId);
        $customers = new Customers();
        $status = $customers->delCustomers($cId);
        if (!$status) {
            $this->error(Error::DEL_ERROR);
        }
        return $this->success('ok', '删除成功');
    }

    /**
     * 回收站的客源信息
     */
    public function recycleBinInfo(Request $request)
    {
        $param = $request->all();
        $where[] = ['c_status', 2];
        if($request->input('start_time')){
            $where[] = ['c_update_at', '>=', strtotime($param['start_time'].' 00:00:00')];
        } else {
            $where[] = ['c_update_at', '>=', strtotime(date('Y-m-d',strtotime('-3 month')).' 00:00:00')];
        }

        if($request->input('end_time')){
            $where[] = ['c_update_at', '<=', strtotime($param['end_time'].' 23:59:59')];
        } else {
            $where[] = ['c_update_at', '<=', time()];
        }

        //关键词智能搜索
        $keywords = '';
        if ($request->input('keywords')){
            //客源编号 客户姓名 客户电话
            $keywords = trim($request->input('keywords'));
        }

        $customers = new Customers();
        $list = $customers->recycleBinList(env('PAGE_LIMIT_ADMIN'),$where,$keywords);
        if (!$list) {
            $data['start'] = date('Y-m-d',strtotime('-3 month'));
            $data['end']  = date('Y-m-d',time());
            return $this->error(Error::NULL_DATA,$data);
        }
        $start = date('Y-m-d',strtotime('-3 month'));
        $end = date('Y-m-d',time());
        return $this->success('ok', $list,$start,$end);
    }


    /**
     * 客源结算业绩分配
     */
    public function addAchievement(Request $request,Customers $customers){

        //$params = $request->all();
        $params = $request->except('token');
        $validator = Validator::make($params, [
            'c_buy_status'   =>'required',//客源状态
            'c_id'            =>'required',//客源Id
            'id'              => 'required',
            'a_type'          => 'required',
            'a_money'         => 'required',
            'a_rate'          => 'required',
            'a_uid'           => 'required',
           ]);

        if ($validator->fails()) {
            return $this->error(Error::PARAM_MISSING);
        }
        $data = $request->except('token','c_secondary_area_name','c_trading_type_name','c_buy_status_name','c_source_type_name','c_entrust_type_name',
            'c_user_level_name','c_live_type_name' ,'c_professional_type_name','c_user_intention_name','c_time_limit_name',
            'c_use_type_name','c_room_type_name','c_toward_name','c_decorate_type_name','c_payment_type_name',
            'c_complete_name','c_pay_type_name','name','u_username', 'customerInfo', 'customerFollow', 'id',
            'a_type', 'a_money', 'a_rate', 'a_uid', 'a_remark');
        $roomId = $request->input('id');//房源Id
        $type = $request->input('a_type');
        $money = $request->input('a_money');
        $rate = $request->input('a_rate');
        $a_uid = $request->input('a_uid');
        $a_remark = $request->input('a_remark');
        $json_info = $request->input('c_room_json','{}');
        $data['c_room_json'] = json_encode($json_info);

        $res = $customers->saveCustomersStatus($data,$roomId,$type, $money, $rate, $a_uid, $a_remark);

        if ($res === -3) {
            //未知错误
            return $this->error(Error::PLEASE_CONTACT_ADMIN);
        }
        if ($res === -1) {
            //人员业绩分配不对等，请仔细检查人员的比例分配
            return $this->error(Error::PROPOR_DIS_MISTAKEN);
        }
        if ($res === -2) {
            //比例分配错误，所有比例总和只能为100
            return $this->error(Error::PROPOR_ONLY_TEN);
        }
        if (!$res) {
            //业绩分配失败
            return $this->error(Error::PROPOR_FAIL);
        }
        return $this->success('ok', '业绩分配成功！');
    }

    /**根据分类ID   获取该分类下面所有的种类 id以及 种类名称
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getAllTypeDetail(Request $request)
    {
        $params = $request->all();
        $vail = AdminVail::getType($params);
        if (!$vail) {
            return $this->error(Error::PARAM_MISSING);
        }
        //根据前端传过来的分类ID 来获取该分类下面的所有种类
        $list =Type::storageRedis($params['t_id']);
        if (!$list) {
            return $this->error(Error::NULL_DATA);
        }
        return $this->success('ok', $list);
    }

    //获取类型
    public function getDefaultType()
    {
        $area_list = DB::table('area')->where(['parentId' => $this->city_code])->get();
        $userInfo = Auth::user();
        $data = [
            'province_code'     => $this->province_code,
            'city_code'         => $this->city_code,
            'city_name'         => $this->city,
            'day'               => date('Y-m-d',time()),//当天时间
            'store_id'          => $userInfo['u_d_id'],//部门ID
            'uid'               => $userInfo['u_id'],//员工ID
            'area_list'         => $area_list,
        ];
        $data['roomtype'] =  SystemType::storageRedis(464);//房型
        $data['hall'] =  SystemType::storageRedis(481);//厅
        $data['guard'] =  SystemType::storageRedis(489);//卫
        $data['balcony'] =  SystemType::storageRedis(497);//阳台
        $data['source'] = SystemType::storageRedis(505); //来源
        $data['signing'] = SystemType::storageRedis(515);//签约方式
        //价格单位
        $data['use'] = SystemType::storageRedis(1);//用途
        $data['grade'] = SystemType::storageRedis(409);//等级
        $data['nowliving'] = SystemType::storageRedis(413);//现住
        $data['type'] = SystemType::storageRedis(420);//职业
        $data['intention'] = SystemType::storageRedis(435); //意向
        $data['term'] = SystemType::storageRedis(440); //期限
        $data['transaction'] =  SystemType::storageRedis(450);//交易
        $data['status'] = SystemType::storageRedis(454);//客源状态
        $data['payment'] = SystemType::storageRedis(519);//付款方式
        $data['commission'] = SystemType::storageRedis(527);//付佣
        $data['residential_type'] = SystemType::storageRedis(537);//住宅类型
        $data['residential_decoration'] = SystemType::storageRedis(552);//客源住宅，商住，其他装修
        $data['matching'] = SystemType::storageRedis(559);//客源--配套--住宅、商住、其他
        $data['oriented'] = SystemType::storageRedis(565);//客源--朝向--住宅、商住、其他
        $data['position'] = SystemType::storageRedis(576);//客源--位置--商铺、铺厂、网点
        $data['typefives'] = SystemType::storageRedis(580);//客源--类型--商铺、铺厂、网点
        $data['wc'] = SystemType::storageRedis(586);//客源--厕所--商铺、铺厂、网点
        $data['lot'] = SystemType::storageRedis(589);//客源--地段--商铺、铺厂、网点
        $data['numberlayers'] = SystemType::storageRedis(594);//客源--层数--商铺、铺厂、网点
        $data['level'] = SystemType::storageRedis(603);//客源--级别--写字楼、写厂
        $data['typeone'] = SystemType::storageRedis(608);//客源--类型--写字楼、写厂
        $data['wcone'] = SystemType::storageRedis(615);//客源--厕所--写字楼、写厂
        $data['matchingone'] = SystemType::storageRedis(618);//客源--配套-写字楼、写厂
        $data['numberlayers_xx'] = SystemType::storageRedis(622);//客源--层数--写字楼、写厂
        $data['typetwo'] = SystemType::storageRedis(631);//客源--类型--厂房、仓库
        $data['fencetwo'] = SystemType::storageRedis(638);//客源--围墙--厂房、仓库
        $data['matchingtwo'] = SystemType::storageRedis(641);//客源--配套--厂房、仓库
        $data['numberlayerstwo'] = SystemType::storageRedis(646);//层数
        $data['planning'] = SystemType::storageRedis(655);//客源--规划--地皮
        $data['typethree'] = SystemType::storageRedis(660);//客源--类型--地皮
        $data['fencethree'] = SystemType::storageRedis(666);//客源--围墙--地皮
        $data['matchingthree'] = SystemType::storageRedis(669);//客源--配套--地皮
        $data['situationthree'] = SystemType::storageRedis(674);//客源--状况--地皮
        $data['positionfour'] = SystemType::storageRedis(679);//客源--位置--车位
        $data['typefour'] = SystemType::storageRedis(685);//客源--类型--车位
        $data['fencefour'] = SystemType::storageRedis(691);//客源--围墙--车位
        $data['matchingfour'] = SystemType::storageRedis(694);//客源--配套--车位
        $data['situationfour'] = SystemType::storageRedis(699);//客源--状况--车位
        $data['rentunit'] = SystemType::storageRedis(704);//房源--租价单位
        return $this->success('ok', $data);
    }

    /**
     * 根据客源ID  查询详情
     */
    public function detail(Request $request)
    {
        if (!$request->input('c_id')) {
            return $this->error(Error::PARAM_MISSING);
        }
        $customers = new Customers();
        $detail = $customers->customersSameList($request->input('c_id'), 1);


        //判断当前登陆员工的权限
//        $userInfo = Auth::user();
//        $authories = explode(',', $userInfo['authories']);//权限=============后期加权限判断
//        $uId = $userInfo['u_id'];//用户ID
//        if(!$detail){
//            return $this->error(Error::INVALID_ARUGMENT);
//        }
//        if ($detail->c_employees_id != $uId  && $detail->c_private_guest == 1){
//            $detail->c_user_phone = '';
//        }
        $detail->c_secondary_area_name    = AreaController::getAreaName($detail->c_secondary_area);//区县
        $detail->c_trading_type_name      = SystemType::getTypeDetail($detail->c_trading_type);//交易类型
        $detail->c_buy_status_name        = SystemType::getTypeDetail($detail->c_buy_status);//购买状态
        $detail->c_source_type_name       = SystemType::getTypeDetail($detail->c_source_type);//来源
        $detail->c_entrust_type_name      = SystemType::getTypeDetail($detail->c_entrust_type);//委托方式
        $detail->c_user_level_name        = SystemType::getTypeDetail($detail->c_user_level);//等级
        $detail->c_live_type_name         = SystemType::getTypeDetail($detail->c_live_type);//现住
        $detail->c_professional_type_name = SystemType::getTypeDetail($detail->c_professional_type);//类别
        $detail->c_user_intention_name    = SystemType::getTypeDetail($detail->c_user_intention);//意向
        $detail->c_time_limit_name        = SystemType::getTypeDetail($detail->c_time_limit);//期限
        $detail->c_use_type_name          = SystemType::getTypeDetail($detail->c_use_type);//用途
        $detail->c_room_type_name         = SystemType::getTypeDetail($detail->c_room_type);//房屋类型
        $detail->c_toward_name            = SystemType::getTypeDetail($detail->c_toward);//朝向
        $detail->c_decorate_type_name     = SystemType::getTypeDetail($detail->c_decorate_type);//装修
        $detail->c_payment_type_name      = SystemType::getTypeDetail($detail->c_payment_type);//付款
        //配套名字 c_complete
        $c_complete_name = array();//配套名字
        $completeArr = explode(',', $detail->c_complete);
        foreach ($completeArr as $ke=>$va) {
            array_push($c_complete_name, SystemType::getTypeDetail($va));
        }
        $detail->c_complete_name = implode(',', $c_complete_name);//配套的名字
        $detail->c_pay_type_name      = SystemType::getTypeDetail($detail->c_pay_type);//付佣
//        $detail->customerInfo = $customers->customersFastInfo($detail->c_id);//快速预览信息与跟进
//        $detail->customerFollow = $customers->fastcCustomerFollow($detail->c_id);//获取跟进最大5条
         return $this->success('ok', $detail);
    }

}